package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.lzy.lzydemo.R;

/**
 * Created by lzy on 2016/4/30.
 */
public class customview1 extends View {
    private Paint paint;
    private Canvas canvas;
    private int textcolor;
    private String text;
    private float textsize;
    private float radius;

    private Rect mTextBound;

    private RectF mColorWheelRectangle = new RectF();

    public customview1(Context context) {
        this(context,null);

    }

    public customview1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);


    }

    public customview1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();
        canvas = new Canvas();
        mTextBound = new Rect();

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
                R.styleable.customview1);
        textcolor = mTypedArray.getColor(R.styleable.customview1_textColor,Color.RED);
        textsize = mTypedArray.getDimension(R.styleable.customview1_textSize, 20);
        radius = mTypedArray.getDimension(R.styleable.customview1_customradius,100);
        text = mTypedArray.getString(R.styleable.customview1_customtext);
        paint.getTextBounds(text,0,text.length(),mTextBound);
    }

//    public customview1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        canvas.drawCircle(getWidth()/2,getWidth()/2,radius,paint);
        paint.setStrokeWidth(10);
        canvas.drawCircle(getWidth()/2,getWidth()/2,5,paint);

        canvas.drawLine(0,getWidth()/2,getWidth(),getWidth()/2,paint);
        canvas.drawRect(0,0,getWidth(),getWidth(),paint);
        paint = new Paint();
        paint.setTextSize(50);
        paint.setColor(textcolor);

        canvas.drawText(text,0,getWidth()+mTextBound.height(),paint);

        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);


        mColorWheelRectangle.set(2,2,
                300, 300);
        canvas.drawArc(mColorWheelRectangle, -90, 360, false, paint);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);
        canvas.drawArc(mColorWheelRectangle, -90, 200, false, paint);
    }
}
